// Copyright@ChenChao

#pragma once

#include "CoreMinimal.h"
#include "Animation/AnimInstance.h"
#include "CAnimInstance.generated.h"

class UCharacterMovementComponent;
/**
 * 
 */
UCLASS()
class CRUNCH_API UCAnimInstance : public UAnimInstance
{
	GENERATED_BODY()

public:

	//本机初始化覆盖点
	virtual void NativeInitializeAnimation() override;

	//本机更新覆盖点。在这一步中简单地收集数据通常是个好主意
	//因为大部分工作都是在NativeThreadSafeUpdateAnimation中完成的。
	virtual void NativeUpdateAnimation(float DeltaSeconds) override;

	//本机线程安全更新覆盖点。在图更新之前在工作线程上执行
	//对于链接的anim实例，仅在宿主节点相关时调用
	virtual void NativeThreadSafeUpdateAnimation(float DeltaSeconds) override;

	//获取速度
	UFUNCTION(BlueprintCallable, meta=(BlueprintThreadSafe))
	FORCEINLINE float GetSpeed() const {return Speed;}

	//获取偏航速度
	UFUNCTION(BlueprintCallable, meta=(BlueprintThreadSafe))
	FORCEINLINE float GetYawSpeed() const {return YawSpeed;}
	
	//获取平滑偏航速度
	UFUNCTION(BlueprintCallable, meta=(BlueprintThreadSafe))
	FORCEINLINE float GetSmoothedYawSpeed() const {return SmoothedYawSpeed;}
	UFUNCTION(BlueprintCallable, meta=(BlueprintThreadSafe))
	FORCEINLINE bool IsMoving() const {return Speed != 0;}
	
	UFUNCTION(BlueprintCallable, meta=(BlueprintThreadSafe))
	FORCEINLINE bool IsNotMoving() const {return Speed == 0;}

	//获取是否跳跃
	UFUNCTION(BlueprintCallable, meta=(BlueprintThreadSafe))
	FORCEINLINE bool GetIsJumpping() const {return bIsJumpping;}
	UFUNCTION(BlueprintCallable, meta=(BlueprintThreadSafe))
	FORCEINLINE bool GetIsOnGround() const {return !bIsJumpping;}
	
	//获取视角旋转偏移
	UFUNCTION(BlueprintCallable, meta=(BlueprintThreadSafe))
	FORCEINLINE float GetLookRotationOffsetYaw() const {return LookRotationOffset.Yaw;}
	UFUNCTION(BlueprintCallable, meta=(BlueprintThreadSafe))
	FORCEINLINE float GetLookRotationOffsetPitch() const {return LookRotationOffset.Pitch;}
	
	
private:
	//所有者角色
	UPROPERTY()
	TObjectPtr<ACharacter> OwnerCharacter;
	//所有者移动组件
	UPROPERTY()
	TObjectPtr<UCharacterMovementComponent> OwnerMovementComponent;
	
	//速度
	UPROPERTY()
	float Speed;
	//偏航速度
	UPROPERTY()
	float YawSpeed;
	//平滑后的偏航速度
	UPROPERTY()
	float SmoothedYawSpeed;
	//平滑插值速度
	UPROPERTY(EditAnywhere,Category="Animation")
	float InterpSpeed = 1.f;	//一个用于平滑处理偏航角速度(Yaw Rate)的算法参数，其核心功能是通过插值(Lerp)实现旋转速度的平滑过渡，避免突变带来的不稳定性
	//身体上一帧旋转
	UPROPERTY()
	FRotator BodyPreviousRotation;
	//旋转视角偏移
	UPROPERTY()
	FRotator LookRotationOffset;
	//是否跳跃
	UPROPERTY()
	bool bIsJumpping;
};
